<template>
  <div class="custom-table">
    <el-table
      ref="multipleTable"
      :height="settings.height"
      v-loading="settings.isLoading"
      @selection-change="(e) => handleClick('select', e)"
      highlight-current-row
      @current-change="handleCurrentChange"
      @cell-click="handleCellclick"
      @select="handleSelect"
      @select-all="handleSelectall"
      :data="data"
      style="width: 100%"
      row-key="id"
      :expand-row-keys="settings.expandrowkeys"
      :indent="0"
      default-expand-all
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column
        v-if="settings.isSelection"
        width="55"
        type="selection"
        fixed
      ></el-table-column>
      <el-table-column
        v-if="settings.isIndex"
        type="index"
        :index="1"
        fixed
        label="序号"
        width="60"
      ></el-table-column>
      <template v-for="item in header">
        <template v-if="item.prop !== 'action' && item.column != true">
          <el-table-column
            :key="item.prop"
            :type="item.type"
            :prop="item.prop"
            :label="item.label"
            :width="item.width"
            :fixed="item.fixed"
            :show-overflow-tooltip="true"
            align="center"
          ></el-table-column>
        </template>
        <template v-if="item.column == true">
          <slot :name="item.prop"></slot>
        </template>
      </template>
      <slot name="action"></slot>
    </el-table>
    <el-pagination
      v-if="settings.isPagination"
      background
      style="text-align:right;padding:25px 15px 25px 0"
      @size-change="(e) => handleClick('pageSize', e)"
      @current-change="(e) => handleClick('currentPage', e)"
      :current-page="currentPage"
      :page-sizes="[20, 50, 100, 200]"
      :page-size="20"
      layout="total, sizes, prev, pager, next, jumper"
      :total="settings.total"
    ></el-pagination>
  </div>
</template>

<script>
/*传值说明:
    settings:{ 相关配置
        isLoading加载数据时显示动效
        height表格高度
        autoHeight:true 是否自动高度
        isSelection; 是否有多选
        selectionWidth多选的宽度
        isIndex 是否需要序列号,默认不需要
        isBorder:是否加框线,默认添加,
        isPagination:是否添加分页,默认false,
        total: 列表总条数
        currentPage 当前页数
    }
    tableData: { 表格数据}
    tableHeader:{表头字段
        isTemplate 是否使用插槽
        isImagePopover 图片带tooltip
    }
事件说明:
    handleClick
    参数 type 点击的类型,如
    选择select, 多选时
    编辑edit, 按钮为编辑时
    查看show, 按钮为查看时
    删除delete, 按钮为删除时
    当前条数pageSize, 开启分页时
    当前页数currentPage等 开启分页时
    e 选中项
    i 选中索引
*/
export default {
  name: "",
  data() {
    return {};
  },
  props: {
    data: { type: Array, default: () => [] },
    // data: { type: Object, default: () => { } },
    header: { type: Array, required: true },
    settings: {
      type: Object,
      default: () => {
        return {
          height: null,
          isBorder: true,
          isLoading: false,
          isIndex: false,
          isSelection: false,
          isPagination: false,
          currentPage: 1,
          total: 20,
        };
      },
    },
  },
  computed: {
    currentPage: function () {
      return this.settings.currentPage;
    },
  },
  mounted() { },
  watch: {
    settings: {
      handler: function (e) {
        console.log(e);
        if (typeof e.isBorder === "undefined") e.isBorder = true;
        if (typeof e.total === "undefined") e.total = 20;
      },
      immediate: true,
    },
    'settings.expandrowkeys'() {
      //展开行手风琴效果设置
      this.data.map((item) => {
        if (this.settings.expandrowkeys[0] != item.id) {
          this.$refs.multipleTable.toggleRowExpansion(item, false)
        } else {
          this.$refs.multipleTable.toggleRowExpansion(item)
        }
      })
    }
  },
  methods: {
    handleClick(type, e) {
      console.log(e);
      if (type == "select") {
        this.$emit("select", e);
      } else if (type == "pageSize") {
        //一页多少条
        this.$emit("pageSize", e);
      } else {
        //第几页
        this.$emit("currentPage", e);
      }
    },
    handleCurrentChange(val) {
      this.$emit("currentChange", val);
    },
    // handleCellclick(row, column, cell, event) {//下面函数可包含参数
    handleCellclick() {
      //切换选中状态
      // this.$refs.multipleTable.toggleRowSelection(row);
    },
    handleSelect(selection, row) {
      //勾选
      this.$emit("handleSelect", selection);
    },
    handleSelectall(selection) {
      //全选
      this.$emit("handleSelectall", selection);
    },
  },
};
</script>
<style scoped>
.custom-table {
  width: 100%;
}

.el-table .el-table__expand-icon {
  display: none;
}

.el-table__row--level-1 {
  background-color: #F0EDFF;
}

.el-table__body tr.current-row>td {
  background-color: #fff !important;
}
</style>
